home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-01-19 | 2.6 KB | 98 lines | [TEXT/MMCC] |
- ===================================
- Tech Note: Metrowerks C++ Templates
- ===================================
-
- Instantiation:
- ==============
-
- MW C++ supports (semi-)automatic and explicit template instantiation:
-
- --File:templ.h-------------
- // template declarations
-
- template <class T> class Templ {
- T member;
- public:
- Templ(T x) { member=x; }
- T Get();
- };
-
- template <class T> T Max(T,T);
- ---------------------------
-
- --File:templ.cp------------
- // template function definitions
-
- #include "templ.h"
-
- template <class T> T Templ<T>::Get() { return member; }
-
- template <class T> T Max(T x,T y) { return (x>y)?x:y; }
- ---------------------------
-
- Automatic instantiation:
- ========================
-
- For automatic instantiation all used template definitions have to be available
- at the end of the translation unit. All required template function instances will
- then be automatically generated:
-
- --File:auto.cp-------------
- #include "templ.cp" // include template declarations & defintions
-
- long foomax(Templ<long> a,Templ<long> b)
- {
- return Max(a.Get(),b.Get());
- }
- // The template functions: "long Max<long>(long,long);" and
- // "long Templ<long>::Get();" will be created as automatic instances
- ---------------------------
-
- Explicit instantiation:
- =======================
-
- MW C++ supports the ANSI C++ syntax and semantics for explicit instantiation:
-
- explicit-intantiation:
- template <inst> ;
-
- inst:
- <class-key> <template-id>
- <type-specifier-seq> <template-id> ( <parameter-declaration-clause> )
-
- For example:
- template class Templ<int>;
- template int Max<int>(int,int);
-
- A declaration of the template must be in scope and the definition of the template
- must be available at the point of explicit instantiation. Explicit instantiation
- of a template class implies the instantiation of all its members not previously
- explicitly specialized.
-
- --File:expl.cp-------------
- #include "templ.h" // include template declarations only
-
- long foomax(Templ<long> a,Templ<long> b)
- {
- return Max(a.Get(),b.Get());
- }
- // no automatic instances will be created
- ---------------------------
-
- --File:inst.cp-------------
- #include "templ.cp" // include template declarations & defintions
-
- template class Templ<long>; // explicit class instantiation
- // will create the member function instance "long Templ<long>::Get();"
-
- template long Max<long>(long,long); // explicit function instantiation
- // will create the function instance "long Max<long>(long,long);"
- ---------------------------
-
- Explicit and automatic instantiation can be freely mixed.
- An explict instantiation will override any automatic instances.
-
- ====================================
-
- Features that are not yet supported:
-